 //
//  HomeMenuTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/4/29.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "HomeMenuTableViewCell.h"

@implementation HomeMenuTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


/**  20  + 90 * count */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.under = [TenGrayShadowView new];
        self.under.backgroundColor = [UIColor themeGrayColor];
        [self addSubview:self.under];
        self.under.sd_layout.bottomEqualToView(self).heightIs(10 * [UIScreen zj_screenHScale]).widthIs([UIScreen zj_screenWith]).leftEqualToView(self);
        
        
       [self.contentView addSubview:self.scrollView];
        [self addSubview:self.pageCotrol];
    }
    return self;
}


#pragma mark - 私有方法

- (void)setData:(NSArray *)data{
    _data = data;
   
    for (UIView *home in self.scrollView.subviews) {
        if ([home isKindOfClass:[HomeImageAndLabelView class]]) {
            [home removeFromSuperview];
        }
    }

    NSInteger yu = data.count % 4;
    NSInteger zheng = data.count / 4;
    if (yu > 0) {
        zheng += 1;
    }
    
    if (data.count > 8) {
        self.pageCotrol.numberOfPages = zheng;
    }else{
        self.pageCotrol.hidden = YES;
    }
    
    self.cwidth = [UIScreen zj_screenWith] / 4;
    self.cheight = 90 * [UIScreen zj_screenHScale];
    
    
    for (int i = 0; i < data.count; i ++) {
        MyBranchMenuModel *model = data[i];
        HomeImageAndLabelView *cenet = [HomeImageAndLabelView new];
        cenet.homeLabel.text = model.name;
        
        [cenet.homeImageView sd_setImageWithURL:CREATEURL(model.iconurl) placeholderImage:PlaceholderImage];
        cenet.userInteractionEnabled = YES;
        cenet.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCenter:)];
        [cenet addGestureRecognizer:tap];
        
        if (i < 4) {
            cenet.frame = CGRectMake(self.cwidth * i, 0, self.cwidth, self.cheight);
        }else if(i < 8){
            
            NSInteger row = i - 4;
            cenet.frame = CGRectMake(self.cwidth * row, self.cheight, self.cwidth, self.cheight);
        }else if (i < 12){
            NSInteger row = i - 8;
            cenet.frame = CGRectMake(self.cwidth * row + [UIScreen zj_screenWith], 0, self.cwidth, self.cheight);
            
        }else{
            NSInteger row = i - 12;
            cenet.frame = CGRectMake(self.cwidth * row  + [UIScreen zj_screenWith], self.cheight, self.cwidth, self.cheight);
        }
        [self.scrollView addSubview:cenet];
        if ([model.link isEqualToString:@"ORG_LIFE"]) {
            cenet.messageNumber.text = [NSString stringWithFormat:@"%@", self.unReads[0]];
            cenet.messageNumber.hidden = NO;
            if ([cenet.messageNumber.text isEqualToString:@"0"]) {
                cenet.messageNumber.hidden = YES;
            }
            
        }else if([model.link isEqualToString:@"STUDY"]){
            cenet.messageNumber.text = [NSString stringWithFormat:@"%@", self.unReads[1]];
            cenet.messageNumber.hidden = NO;
            if ([cenet.messageNumber.text isEqualToString:@"0"]) {
                cenet.messageNumber.hidden = YES;
            }
        }else if([model.link isEqualToString:@"GY"]){
            cenet.messageNumber.text = [NSString stringWithFormat:@"%@", self.unReads[2]];
            cenet.messageNumber.hidden = NO;
            if ([cenet.messageNumber.text isEqualToString:@"0"]) {
                cenet.messageNumber.hidden = YES;
            }
        }else if([model.link isEqualToString:@"NEWS_TOTAL"]){
            cenet.messageNumber.text = [NSString stringWithFormat:@"%@", self.unReads[3]];
            cenet.messageNumber.hidden = NO;
            if ([cenet.messageNumber.text isEqualToString:@"0"]) {
                cenet.messageNumber.hidden = YES;
            }
        }
        
        
        if(i == 3){
            cenet.messageNumber.text = [NSString stringWithFormat:@"%@", self.unReads[3]];
            cenet.messageNumber.hidden = NO;
            if ([cenet.messageNumber.text isEqualToString:@"0"]) {
                cenet.messageNumber.hidden = YES;
            }
        }

        
    }
    
    
    if (data.count < 9) {
        self.scrollView.contentSize = CGSizeMake([UIScreen zj_screenWith], self.scollViewHeight);
    }else{
        self.scrollView.contentSize = CGSizeMake([UIScreen zj_screenWith] * 2, self.scollViewHeight);
    }
}

#pragma mark - 监听事件
- (void)tapCenter:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(homeMenuWithCell:section:index:)]) {
        [self.delegate homeMenuWithCell:self section:self.section index:tap.view.tag];
    }
}

#pragma mark - 协议代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.pageCotrol.currentPage = scrollView.contentOffset.x / [UIScreen zj_screenWith];
}

#pragma mark - getter
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 180 * [UIScreen zj_screenHScale])];
        _scrollView.delegate = self;
        _scrollView.contentOffset = CGPointMake(0, 0);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        
    }
    return _scrollView;
    
}


- (UIPageControl *)pageCotrol{
    if (!_pageCotrol) {
        _pageCotrol = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.scollViewHeight, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
        _pageCotrol.currentPage = 0;
        _pageCotrol.backgroundColor = [UIColor clearColor];
        _pageCotrol.pageIndicatorTintColor = [UIColor themeGrayColor];
        _pageCotrol.currentPageIndicatorTintColor = [UIColor themeColor];
        
    }
    return _pageCotrol;
}
@end
